Ext.define('Ssimgres.controller.Articulo', {
	extend : 'Ext.app.Controller',
	views : ['articulo.ArticulosView', 'articulo.ArticuloView', 'articulo.CortarAvatarArticuloView'],
	stores : ['Articulos', 'CategoriasArticulos'],
	models : ['Articulo'],
	refs : [{
		ref : 'articulosView',
		selector : 'articulosView'
	}, {
		ref : 'articuloView',
		selector : 'articuloView'
	}, {
		ref : 'cortarAvatarArticuloView',
		selector : 'cortarAvatarArticuloView'
	}],
	init : function() {
		this.control({
			'articulosView' : {
				itemclick : this.activarBotones
			},
			'articulosView button[action=btnNuevo]' : {
				click : this.articuloViewShow
			},
			'articulosView button[action=btnEliminar]' : {
				click : this.eliminarTipo
			},
			'articulosView button[action=btnEditar]' : {
				click : this.articuloViewShowEditar
			},
			'articuloView button[action=btnCancelar]' : {
				click : this.articuloViewCancelar
			},
			'cortarAvatarArticuloView button[action=btnCancelar]' : {
				click : this.cortarAvatarArticuloViewCancelar
			},
			'cortarAvatarArticuloView button[action=btnAceptar]' : {
				click : this.cortarAvatarArticuloViewAceptar
			},
			'articuloView button[action=btnGuardar]' : {
				click : this.articuloViewGuardar
			},
			'articuloView filefield[action=btnExaminar]' : {
				change : this.avatarUpload
			}
		});
	},
	reset : function() {
		Ext.getCmp('articulosView_btnEditar').setDisabled(true);
		Ext.getCmp('articulosView_btnEliminar').setDisabled(true);
		this.getArticulosStore().load();
		this.getArticulosView().getSelectionModel().clearSelections();
	},
	articuloViewShow : function() {
		var Articulo = this.getArticuloModel();
		var articulo = new Articulo();

		this.getCategoriasArticulosStore().load();

		Ext.widget('articuloView').show();

		var form = this.getArticuloView().down('form').getForm();
		var avatar = Ext.getCmp('articuloView_avatarArticulo');

		form.loadRecord(articulo);
		avatar.setSrc('/servicio_articulo/avatar/');
		Ext.getCmp('articuloView_mostrar').setValue(true);
	},
	articuloViewShowEditar : function() {
		this.getCategoriasArticulosStore().load();

		Ext.widget('articuloView').show();

		var form = this.getArticuloView().down('form').getForm();
		var record = this.getArticulosView().getSelectionModel().getSelection()[0];
		var avatar = Ext.getCmp('articuloView_avatarArticulo');

		form.loadRecord(record);
		avatar.setSrc('/servicio_articulo/avatar/' + record.getId() + "?" + new Date().getTime());
		
		Ext.getCmp('articuloView_mostrar').setValue(record.get('mostrar_avatar'));
		
	},
	activarBotones : function() {
		Ext.getCmp('articulosView_btnEditar').setDisabled(false);
		Ext.getCmp('articulosView_btnEliminar').setDisabled(false);
	},
	articuloViewCancelar : function() {
		this.getArticuloView().close();
	},
	cortarAvatarArticuloViewCancelar : function() {
		this.getCortarAvatarArticuloView().close();
	},
	cortarAvatarArticuloViewAceptar : function() {
		Ext.Ajax.request({
			url : '/servicio_img_temp/crop_resize/' + this.getCortarAvatarArticuloView().img_temp_id,
			method : 'post',
			params : {
				x : this.getCortarAvatarArticuloView().cropData.x,
				y : this.getCortarAvatarArticuloView().cropData.y,
				width : this.getCortarAvatarArticuloView().cropData.width,
				height : this.getCortarAvatarArticuloView().cropData.height,				
				size_width : 350,
				size_height : 238
			},
			success : function(response) {
				var respuesta = Ext.JSON.decode(response.responseText);
				if (respuesta.success) {
					this.getCortarAvatarArticuloView().close();

					var avatar = Ext.getCmp('articuloView_avatarArticulo');
					avatar.setSrc('/servicio_img_temp/img/' + respuesta.img_temp_id);

					var form = this.getArticuloView().down('form').getForm();
					var record = form.getRecord();

					record.set('img_temp_id', respuesta.img_temp_id);
				} else {
					Ext.Msg.show({
						title : 'Error',
						msg : respuesta.msg,
						buttons : Ext.Msg.OK,
						icon : Ext.Msg.ERROR
					});
				}
			},
			scope : this
		});
	},
	avatarUpload : function(button, value) {
		var form = button.up('form').getForm();
		var ext = value.substring(value.lastIndexOf('.') + 1).toLowerCase();
		if (value != '' && ext != 'png' && ext != 'jpg' && ext != 'gif') {
			Ext.Msg.show({
				title : 'Atención',
				msg : 'Formato de archivo no soportado',
				buttons : Ext.Msg.OK,
				icon : Ext.Msg.WARNING
			});
		} else {
			form.submit({
				scope : this,
				url : '/servicio_img_temp/guardar',
				waitMsg : 'Cargando imagen...',
				success : function(form, action) {
					Ext.widget('cortarAvatarArticuloView', {
						imageUrl : '/servicio_img_temp/img/' + action.result.img_temp_id,
						img_temp_id : action.result.img_temp_id
					}).show();
				},
				failure : function(form, action) {
					Ext.Msg.show({
						title : 'Error',
						msg : action.result.msg,
						buttons : Ext.Msg.OK,
						icon : Ext.Msg.ERROR
					});
				}
			});
		}
	},
	articuloViewGuardar : function() {
		var form = this.getArticuloView().down('form').getForm();
		var mostrar = Ext.getCmp('articuloView_mostrar').getValue();

		if (form.isValid()) {
			form.updateRecord();
			
			form.getRecord().set('mostrar_avatar',mostrar);
			
			form.getRecord().save({
				success : function(operation) {
					Ext.Msg.show({
						title : 'Éxito',
						msg : operation.proxy.reader.jsonData["msg"],
						buttons : Ext.Msg.OK,
						icon : Ext.Msg.INFO
					});
					this.getArticuloView().close();
					this.reset();
				},
				failure : function(operation) {
					Ext.Msg.show({
						title : 'Error',
						msg : operation.proxy.reader.jsonData["msg"],
						buttons : Ext.Msg.OK,
						icon : Ext.Msg.ERROR
					});
				},
				scope : this
			});
		} else {
			Ext.Msg.show({
				title : 'Atención',
				msg : 'Verifica los campos del formulario',
				buttons : Ext.Msg.OK,
				icon : Ext.Msg.WARNING
			});
		}

	},
	eliminarTipo : function() {
		var record = this.getArticulosView().getSelectionModel().getSelection()[0];

		Ext.Msg.confirm('Eliminar registro', '¿Está seguro de la operación?', function(id, value) {
			if (id === 'yes') {
				record.destroy({
					success : function(operation) {
						Ext.Msg.show({
							title : 'Éxito',
							msg : operation.proxy.reader.jsonData["msg"],
							buttons : Ext.Msg.OK,
							icon : Ext.Msg.INFO
						});
						this.reset();
					},
					failure : function(operation) {
						Ext.Msg.show({
							title : 'Error',
							msg : operation.proxy.reader.jsonData["msg"],
							buttons : Ext.Msg.OK,
							icon : Ext.Msg.ERROR
						});
					},
					scope : this
				});
			}
		}, this);

	}
});
